package com.z.redis.command.impl;

import com.z.redis.RedisCore;
import com.z.redis.command.Command;
import com.z.redis.command.CommandType;
import com.z.redis.datatype.BytesWrapper;
import com.z.redis.resp.BulkString;
import com.z.redis.resp.Resp;
import com.z.redis.resp.SimpleString;
import com.z.redis.util.TRACEID;
import io.netty.channel.ChannelHandlerContext;

/**
 * 设置当前连接的名称 (setname)
 */
public class Client implements Command {
    private String subCommand;
    private Resp[] array;

    @Override
    public CommandType type(){
        return CommandType.client;
    }

    @Override
    public void setContent(Resp[] array){
        this.array = array;
        subCommand = ((BulkString) array[1]).getContent().toUTF8String();
    }

    @Override
    public void handle(ChannelHandlerContext ctx, RedisCore redisCore){
        String traceId = TRACEID.currentTraceId();
        LOGGER.debug("traceId:{}, client command:{}"+traceId+subCommand);
        switch (subCommand){
            case "setname":
                BytesWrapper connectionName = ((BulkString) array[2]).getContent();
                redisCore.putClient(connectionName, ctx.channel());
                break;
            default:
                throw new IllegalStateException();
        }
        ctx.writeAndFlush(SimpleString.OK);
    }
}
